package com.it.smartoldb.repository;

import com.it.smartoldb.entity.HealthRecord;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 健康记录数据访问层
 */
@Repository
public interface HealthRecordRepository extends JpaRepository<HealthRecord, Long> {
    
    /**
     * 根据老人ID查找健康记录
     */
    Page<HealthRecord> findByElderlyId(Long elderlyId, Pageable pageable);
    
    /**
     * 根据记录类型查找
     */
    Page<HealthRecord> findByRecordType(String recordType, Pageable pageable);
    
    /**
     * 根据老人ID和记录类型查找
     */
    Page<HealthRecord> findByElderlyIdAndRecordType(Long elderlyId, String recordType, Pageable pageable);
    
    /**
     * 根据时间范围查找记录
     */
    @Query("SELECT hr FROM HealthRecord hr WHERE hr.elderlyId = :elderlyId " +
           "AND hr.recordTime BETWEEN :startTime AND :endTime " +
           "ORDER BY hr.recordTime DESC")
    List<HealthRecord> findByElderlyIdAndTimeRange(@Param("elderlyId") Long elderlyId,
                                                   @Param("startTime") LocalDateTime startTime,
                                                   @Param("endTime") LocalDateTime endTime);
    
    /**
     * 查找最新的记录
     */
    @Query("SELECT hr FROM HealthRecord hr WHERE hr.elderlyId = :elderlyId " +
           "ORDER BY hr.recordTime DESC")
    Page<HealthRecord> findLatestByElderlyId(@Param("elderlyId") Long elderlyId, Pageable pageable);
    
    /**
     * 根据状态查找记录
     */
    Page<HealthRecord> findByStatus(String status, Pageable pageable);
    
    /**
     * 复合条件查询
     */
    @Query("SELECT hr FROM HealthRecord hr WHERE " +
           "(:elderlyId IS NULL OR hr.elderlyId = :elderlyId) AND " +
           "(:recordType IS NULL OR hr.recordType = :recordType) AND " +
           "(:status IS NULL OR hr.status = :status) AND " +
           "(:startTime IS NULL OR hr.recordTime >= :startTime) AND " +
           "(:endTime IS NULL OR hr.recordTime <= :endTime) " +
           "ORDER BY hr.recordTime DESC")
    Page<HealthRecord> findByConditions(@Param("elderlyId") Long elderlyId,
                                        @Param("recordType") String recordType,
                                        @Param("status") String status,
                                        @Param("startTime") LocalDateTime startTime,
                                        @Param("endTime") LocalDateTime endTime,
                                        Pageable pageable);
    
    /**
     * 根据记录类型统计数量
     */
    long countByRecordType(String recordType);
    
    /**
     * 根据状态统计数量
     */
    long countByStatus(String status);
    
    /**
     * 按日期统计健康记录数量（用于趋势图）
     */
    @Query("SELECT DATE(hr.recordTime) as recordDate, COUNT(hr) as count " +
           "FROM HealthRecord hr " +
           "WHERE hr.recordTime >= :startDate " +
           "GROUP BY DATE(hr.recordTime) " +
           "ORDER BY recordDate")
    List<Object[]> countByDateRange(@Param("startDate") LocalDateTime startDate);
} 